/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#define _TOUCH_MODULE_
#include "touch.h"
#include "gd32f1x0_conf.h"
#include "systimer.h"

/*****************************************************************************//*!
* @brief   	touch modle init .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void TouchInit(void)
{
	/* Configure the GPIO ports */
    GPIO_InitPara  GPIO_InitStructure;
	
	RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_TSI, ENABLE);
	
	/* Connect PXx to USARTx_Tx */
    GPIO_PinAFConfig(GPIOB, GPIO_PINSOURCE0, GPIO_AF_3);
    /* Connect PXx to USARTx_Rx */
    GPIO_PinAFConfig(GPIOB, GPIO_PINSOURCE1, GPIO_AF_3);

    /* Configure USART Rx/Tx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin     = GPIO_PIN_0 | GPIO_PIN_1;
    GPIO_InitStructure.GPIO_Mode    = GPIO_MODE_AF;
    GPIO_InitStructure.GPIO_Speed   = GPIO_SPEED_50MHZ;
    GPIO_InitStructure.GPIO_OType   = GPIO_OTYPE_PP;
    GPIO_InitStructure.GPIO_PuPd    = GPIO_PUPD_NOPULL;
    GPIO_Init(GPIOB , &GPIO_InitStructure);
	
	TSI->CTLR = TSI_CTCDIV_1 |
				TSI_CTDT_2CTCLK |
				TSI_CDT_2CTCLK |
				TSI_SOFTWARE_TRIGGLER |
				TSI_RISING_EDGE_TRIGGLER |
				TSI_MCN_4095_CYCLE;
	//disable int
	TSI->IER = 0;
	//clear int flag
	TSI->CEFR = 0x03;
	//
	TSI->PHMR = 0;
	//switch control
	TSI->ASWR = 0;
	//enable group
	TSI->GCTLR = 0x04;
	//Sample Pin set
	TSI->SPR = TSI_GROUP3_PIN2;
	//Channel Pin set
	TSI->CPR = TSI_GROUP3_PIN3;
	
	TSI_Enable( ENABLE);
    
	TisOffset = 4;
    TisKeyCnt = 0;
    
	TisIndex = TIS_BUFF_SIZE;
	while(TisIndex--)
	  TisVal[TisIndex] = 0;
	
}

/*****************************************************************************//*!
* @brief   	touch modle check .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void TouchCheck(void)
{
	uint16_t min,max;
    uint16_t Cnt;
    
	if(bPulse_100ms)
	{
		//clear int flag
		TSI->CEFR = 0x03;
		TSI_StartAcquisition( ENABLE );
	}
	
	if(TSI->STR)
	{
        TisIndex++;
        if(TisIndex >= TIS_BUFF_SIZE)
            TisIndex = 0;
        
		TisVal[TisIndex] = TSI->GXCYCNR[2];
		TSI->CEFR = 0x03;	
        
        min = 0xFFFF;
        max = 0;
        for(Cnt = 0;Cnt < TisIndex;Cnt++)
        {
            if( (TisVal[Cnt] > TIS_VAL_MIN) && (TisVal[Cnt] < TIS_VAL_MAX))
            {
                if(min > TisVal[Cnt])
                  min = TisVal[Cnt];
                if(max < TisVal[Cnt])
                  max = TisVal[Cnt];
            }
        }

        if(TisVal[TisIndex] > (min + TisOffset))
            TisKeyVal |= 0x0001;
        if(TisVal[TisIndex] < (max - TisOffset))
            TisKeyVal &= ~0x0001;
	}
    
    if(TisKeyVal != TisKeyValLast)
    {
        TisKeyCnt ++;
    }
    TisKeyValLast = TisKeyVal;
}
